//
//  WBMeDetailViewController.m
//  WeiBo
//
//  Created by wbs on 17/3/13.
//  Copyright © 2017年 xiaomaolv. All rights reserved.
//

#import "WBMeDetailViewController.h"
#import "WBAccountTool.h"
#import "WBAccount.h"
#import "WBUser.h"

#define kHeaderViewHeight 228.0f
#define kShowToolbarHeight 44.0f
#define kStartChangStatusY 60.0f
#define kEndChangStatusY (kHeaderViewHeight - kNavBarAddStatusBarHeight)

@interface WBMeDetailViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UITableView *tableView_Main;
@property (nonatomic, weak) UITableView *tableView_Status;
@property (nonatomic, weak) UIImageView *bgImageView;
@property (nonatomic, weak) UIView *topView;
//@property (nonatomic, strong) UIButton *returnBtn;
//@property (nonatomic, strong) UIButton *searchBtn;
//@property (nonatomic, strong) UIButton *moreBtn;

@property (nonatomic, assign) CGFloat pullDownScale;

@end

@implementation WBMeDetailViewController

- (void)dealloc
{
    kWBLogMothodFunc;
}

- (void)loadView
{
    [super loadView];
    
    WBMeDetailView *detailView = [[WBMeDetailView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = detailView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // UI
    [self loadUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.tableView_Main.contentOffset.y) {
        [self scrollViewDidScroll:self.tableView_Main];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] alpha:1.0] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage resizedImageWithImageStr:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:kSystemNavigationBarLineColor alpha:1.0]];
}

- (void)loadUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = [WBAccountTool account].user.name;
    UIBarButtonItem *rightBarButtonItemSearch = [UIBarButtonItem itemWithImageName:@"userinfo_tabicon_search" highlightImageName:@"userinfo_tabicon_search_highlighted" target:self action:@selector(search)];
    UIBarButtonItem *rightBarButtonItemMore = [UIBarButtonItem itemWithImageName:@"userinfo_tabicon_more" highlightImageName:@"userinfo_tabicon_more_highlighted" target:self action:@selector(more)];
    self.navigationItem.rightBarButtonItems = @[rightBarButtonItemMore, rightBarButtonItemSearch];
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    scrollView.bounces = NO;
    scrollView.contentSize = CGSizeMake(kScreenWidth * 2, 0);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    UITableView *tableView_Main = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    tableView_Main.backgroundColor = [UIColor clearColor];
    tableView_Main.dataSource = self;
    tableView_Main.delegate = self;
    [self.scrollView addSubview:tableView_Main];
    self.tableView_Main = tableView_Main;
    
    UIView *headerView_Main = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeaderViewHeight + kShowToolbarHeight)];
    headerView_Main.backgroundColor = [UIColor clearColor];
    self.tableView_Main.tableHeaderView = headerView_Main;
    self.tableView_Main.scrollIndicatorInsets = UIEdgeInsetsMake(kHeaderViewHeight, 0, 0, 0);
    
    UITableView *tableView_Status = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    tableView_Status.backgroundColor = [UIColor clearColor];
    tableView_Status.dataSource = self;
    tableView_Status.delegate = self;
    [self.scrollView addSubview:tableView_Status];
    self.tableView_Status = tableView_Status;
    
    UIView *headerView_Status = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeaderViewHeight + kShowToolbarHeight)];
    headerView_Status.backgroundColor = [UIColor clearColor];
    self.tableView_Status.tableHeaderView = headerView_Main;
    self.tableView_Status.scrollIndicatorInsets = UIEdgeInsetsMake(kHeaderViewHeight, 0, 0, 0);
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kHeaderViewHeight + kShowToolbarHeight - kScreenWidth, kScreenWidth, kScreenWidth)];
    bgImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"profile_cover_background@2x" ofType:@"jpg"]];
    [self.view addSubview:bgImageView];
    self.bgImageView = bgImageView;

    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeaderViewHeight + kShowToolbarHeight)];
//    topView.backgroundColor = [UIColor redColor];
    [self.view addSubview:topView];
    self.topView = topView;
    
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeaderViewHeight, kScreenWidth, kShowToolbarHeight)];
    showView.backgroundColor = [UIColor blueColor];
    [topView addSubview:showView];
    
    self.pullDownScale = (kScreenHeight - kHeaderViewHeight - kShowToolbarHeight) / (kScreenHeight - kHeaderViewHeight);
}

- (void)search
{
    
}
- (void)more
{
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 28;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView_Main) {
        static NSString *cellID = @"tableView_Main";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"Main:%ld", indexPath.row];
        return cell;
    } else if (tableView == self.tableView_Status) {
        static NSString *cellID = @"tableView_Status";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"Status:%ld", indexPath.row];
        return cell;
    } else {
        static NSString *cellID = @"tableView";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.tableView_Main || scrollView == self.tableView_Status) {
        CGFloat y = scrollView.contentOffset.y;
        if (y <= 0) {
            self.tableView_Main.scrollIndicatorInsets = UIEdgeInsetsMake(kHeaderViewHeight - y, 0, 0, 0);
            self.tableView_Status.scrollIndicatorInsets = UIEdgeInsetsMake(kHeaderViewHeight - y, 0, 0, 0);
            self.bgImageView.transform = CGAffineTransformMakeTranslation(0, - y * self.pullDownScale);
            self.topView.transform = CGAffineTransformMakeTranslation(0, - y);
        } else {
            if (y >= kHeaderViewHeight - kNavBarAddStatusBarHeight) {
                self.bgImageView.transform = CGAffineTransformMakeTranslation(0, -kNavBarAddStatusBarHeight - kHeaderViewHeight);
                self.topView.transform = CGAffineTransformMakeTranslation(0, kNavBarAddStatusBarHeight - kHeaderViewHeight);
            } else {
                self.bgImageView.transform = CGAffineTransformMakeTranslation(0, -y);
                self.topView.transform = CGAffineTransformMakeTranslation(0, -y);
            }
            
            CGFloat alpha = 0.0;
            if (y >= kStartChangStatusY && y <= kEndChangStatusY) {
                alpha = (y - kStartChangStatusY) / (kEndChangStatusY - kStartChangStatusY);
                if (alpha >= 0.0 && alpha <= 0.99) {
                    UIImage *image = [UIImage imageWithColor:kSystemNavigationBarBackgroundColor alpha:alpha];
                    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
                    image = [UIImage imageWithColor:kSystemNavigationBarLineColor alpha:alpha];
                    [self.navigationController.navigationBar setShadowImage:image];
                } else if (alpha > 0.99) {
                    [self.navigationController.navigationBar setBackgroundImage:[UIImage resizedImageWithImageStr:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
                    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:kSystemNavigationBarLineColor alpha:1.0]];
                } else {
                    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] alpha:1.0] forBarMetrics:UIBarMetricsDefault];
                    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
                }
            } else if (y > kEndChangStatusY) {
                [self.navigationController.navigationBar setBackgroundImage:[UIImage resizedImageWithImageStr:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
                [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:kSystemNavigationBarLineColor alpha:1.0]];
            } else {
                [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] alpha:1.0] forBarMetrics:UIBarMetricsDefault];
                [self.navigationController.navigationBar setShadowImage:[UIImage new]];
            }
        }
    }
    
    if (scrollView == self.scrollView) {
        CGFloat x = scrollView.contentOffset.x;
        
        NSLog(@"%f", x);
    }
    
}

- (UIButton *)buttonWithName:(NSString *)name image:(NSString *)imageName highlight:(NSString *)highlightName
{
    UIImage *image = [UIImage imageNamed:imageName];
    UIButton *button = [[UIButton alloc] init];
    CGSize textSize;
    if (name) {
        textSize = [name sizeWithAttributes:@{NSFontAttributeName : kSystemNavigationItemFont}];
        [button setTitle:name forState:UIControlStateNormal];
    }
    button.frame = (CGRect){{0, 0}, {image.size.width + textSize.width, image.size.height}};
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:highlightName] forState:UIControlStateHighlighted];
    return button;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


@implementation WBMeDetailView




@end
